package com.ruoyi.sbgl.sbdb.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.WordUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sbgl.sbdb.domain.Sbdb;
import com.ruoyi.sbgl.sbdb.domain.SbdbDbmx;
import com.ruoyi.sbgl.sbdb.service.ISbdbService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 设备调拨Controller
 *
 * @author huacai
 * @date 2024-09-13
 */
@RestController
@RequestMapping("/sbgl/sbdb")
public class SbdbController extends BaseController {
    @Resource
    private ISbdbService sbdbService;

    /**
     * 查询设备调拨列表
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbdb:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sbdb sbdb) {
        startPage();
        List<Sbdb> list = sbdbService.selectSbdbList(sbdb);
        return getDataTable(list);
    }

    /**
     * 导出设备调拨列表
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbdb:export')")
    @Log(title = "设备调拨", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sbdb sbdb) {
        List<Sbdb> list = sbdbService.selectSbdbList(sbdb);
        List<Sbdb> resultList = new ArrayList<>();
        for(Sbdb item : list) {
            Sbdb result = sbdbService.selectSbdbBySbdbId(item.getSbdbId());
            result.setSbdbDbmxList(result.getSbdbDbmxList()); // 设置查询到的子表数据到主表对象中
            resultList.add(result); // 将查询结果添加到结果集合中
        }
        ExcelUtil<Sbdb> util = new ExcelUtil<Sbdb>(Sbdb.class);
        util.exportExcel(response, resultList, "设备调拨数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Sbdb> util = new ExcelUtil<Sbdb>(Sbdb.class);
        util.importTemplateExcel(response, "设备调拨数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "设备调拨", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sbgl:sbdb:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Sbdb> util = new ExcelUtil<Sbdb>(Sbdb.class);
        InputStream inputStream = file.getInputStream();
        List<Sbdb> list = util.importExcel(inputStream);
        inputStream.close();
        int count = sbdbService.batchInsertSbdb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取设备调拨详细信息
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbdb:query')")
    @GetMapping(value = "/{sbdbId}")
    public AjaxResult getInfo(@PathVariable("sbdbId") Integer sbdbId) {
        return success(sbdbService.selectSbdbBySbdbId(sbdbId));
    }

    /**
     * 新增设备调拨
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbdb:add')")
    @Log(title = "设备调拨", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sbdb sbdb) {
        return toAjax(sbdbService.insertSbdb(sbdb));
    }

    /**
     * 修改设备调拨
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbdb:edit')")
    @Log(title = "设备调拨", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sbdb sbdb) {
        return toAjax(sbdbService.updateSbdb(sbdb));
    }

    /**
     * 删除设备调拨
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbdb:remove')")
    @Log(title = "设备调拨", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sbdbIds}")
    public AjaxResult remove(@PathVariable Integer[] sbdbIds) {
        return toAjax(sbdbService.deleteSbdbBySbdbIds(sbdbIds));
    }

    /**
     * 根据模板导出word
     *
     * @param response
     * @return
     */
    @Log(title = "根据模板导出word", businessType = BusinessType.EXPORT)
    @GetMapping("/exportWord/{sbdbId}")
    public AjaxResult exportWord(@PathVariable("sbdbId") Integer sbdbId, HttpServletRequest request, HttpServletResponse response) {
        Sbdb sbdb = sbdbService.selectSbdbBySbdbId(sbdbId);
        // 创建一个 SimpleDateFormat 对象，并指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date ckRqDate = sbdb.getCkRq();
        String ckRq = sdf.format(ckRqDate); //调拨日期
        String dbZt = sbdb.getDbZt(); //调拨状态
        String ckName = sbdb.getCkName(); //调拨人

        //调拨明细
        List<SbdbDbmx> sbdbDbmxList = sbdb.getSbdbDbmxList();

        Map<String, Object> map = new HashMap<>();

        map.put("ckRq", ckRq);
        map.put("dbZt", dbZt);
        map.put("ckName", ckName);
        map.put("sbdbDbmxList", sbdbDbmxList);

        String str = UUID.randomUUID().toString() + ".docx";
        //获取yml配置地址
        String tempDir = RuoYiConfig.getProfile() + "/upload/";
        String name = WordUtil.easyPoiExport("static/word/sbdb.docx", tempDir, str, map, request, response);
        return AjaxResult.success(name);
    }

}
